<template>
  <el-select
    v-model="purchaseMenu.purchaseId"
    filterable
    remote
    reserve-keyword
    placeholder="请输入采购单id，采购人id，或采购人名字"
    :remote-method="getPurchaseList"
    :loading="loading">
    <el-option
      v-for="item in purchaseMenuList"
      :key="item.id"
      :label="item.assigneeName"
      :value="item.id">
    </el-option>
  </el-select>
</template>

<script>
import purchaseApi from '../../../../api/ware/purchaseApi'

export default {
  name: 'selectAndSearchPurchaseComponent',
  props: ['purchaseId'],
  data () {
    return {
      // 采购单
      purchaseMenu: {
        currentPage: 1,
        size: 10,
        purchaseId: null
      },
      purchaseMenuList: [],
      loading: false,

    }
  },
  methods: {
    // 搜索采购单
    async getPurchaseList (key) {

      let id = null
      let assigneeName = null
      if (key) {
        if (/^[0-9]+$/.test(key)) {
          id = key // 后端的id字段即封装了采购单id也封装了采购人id
        } else if (key.length > 0) {
          assigneeName = key
        }
      }
      this.loading = true
      /**
       * 注意这里只获取了 状态是刚创建或者是刚分配的采购单
       *
       * 采购单的状态要小于等于某一个状态
       * 采购单状态[0新建，1已分配，2正在采购，3已完成，4采购失败]
       *
       * */
      let p = await purchaseApi.getPurchaseList(this.purchaseMenu.currentPage, this.purchaseMenu.size, id, assigneeName,1)

      this.purchaseMenuList = p.data.page.records

      this.loading = false
    },

  },
  created () {
    this.getPurchaseList()
  },
  watch: {
    purchaseId (val) {
      this.purchaseMenu.purchaseId = val

    },
    'purchaseMenu.purchaseId' (purchaseId) {
      this.$emit('update-purchase-id', purchaseId)
    }
  }
}
</script>

<style scoped>

</style>
